<template>
	<view>
		<image src="/static/work1.jpg" mode="" class="img_bg"></image>
		<view class="" style="position: absolute; top: 0; width: 100%;">
			<!-- #ifdef APP-PLUS -->
			<view class=" margin-t30 margin-l30 margin-r30">
				<block v-for="(item,index) in list" :key="index">

					<view class="card margin-t30 relative" @tap="toDetail(item.file_url)">
						<view class="text-36">{{item.filename?item.filename:"-"}}</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">上传时间</view>
							<view>{{item.add_time?item.add_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">更新时间</view>
							<view>{{item.update_time?item.update_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">内容纲要</view>
							<view>{{item.content?item.content:"-"}}</view>
						</view>
					</view>
					<!-- <view class="" style="text-align: center;">
					<uni-link :href="item.file_url" :text="item.filename+' *若无法打开文件请点击此处'">
					</uni-link>
					<view style="font-size: 10upx;"></view>
					</view> -->
				</block>
				<view class="loading" v-show="!isShow">
					{{loadingText}}
				</view>
				<view class="text-center noNotice" v-show="isShow" style="margin-top: 200upx;">
					<image src="/static/notice.png" class="noticeImg"></image>
					<view class="font-gray margin-t30">暂无文件~</view>
				</view>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<view class=" margin-t30 margin-l30 margin-r30">
				<block v-for="(item,index) in list" :key="index">

					<view class="card margin-t30 relative" @tap="toDetail(item.file_url)">
						<view class="text-36">{{item.filename?item.filename:"-"}}</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">上传时间</view>
							<view>{{item.add_time?item.add_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">更新时间</view>
							<view>{{item.update_time?item.update_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">内容纲要</view>
							<view>{{item.content?item.content:"-"}}</view>
						</view>
					</view>
					<!-- <view class="" style="text-align: center;">
					<uni-link :href="item.file_url" :text="item.filename+' *若无法打开文件请点击此处'">
					</uni-link>
					<view style="font-size: 10upx;"></view>
					</view> -->
				</block>
				<view class="loading" v-show="!isShow">
					{{loadingText}}
				</view>
				<view class="text-center noNotice" v-show="isShow" style="margin-top: 200upx;">
					<image src="/static/notice.png" class="noticeImg"></image>
					<view class="font-gray margin-t30">暂无文件~</view>
				</view>
			</view>
			<!-- #endif -->
			<!-- #ifdef MP-ALIPAY -->
			<view class=" margin-t30 margin-l30 margin-r30">
				<block v-for="(item,index) in list" :key="index">
					<view class="card margin-t30 relative" @tap="toDetail(item.file_url)">
						<view class="text-36">{{item.filename?item.filename:"-"}}</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">上传时间</view>
							<view>{{item.add_time?item.add_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">更新时间</view>
							<view>{{item.update_time?item.update_time:"-"}}</view>
						</view>
						<view class="display justify-between margin-t22 text-22">
							<view class="font-gray1 ">内容纲要</view>
							<view>{{item.content?item.content:"-"}}</view>
						</view>
					</view>
					<!-- <view class="" style="text-align: center;">
					<uni-link :href="item.file_url" :text="item.filename+' *若无法打开文件请点击此处'">
					</uni-link>
					<view style="font-size: 10upx;"></view>
					</view> -->
				</block>
				<view class="loading" v-show="!isShow">
					{{loadingText}}
				</view>
				<view class="text-center noNotice" v-show="isShow" style="margin-top: 200upx;">
					<image src="/static/notice.png" class="noticeImg"></image>
					<view class="font-gray margin-t30">暂无文件~</view>
				</view>
			</view>
			<!-- #endif -->


		</view>
	</view>
</template>

<script>
	import uniLink from "@/components/uni-link/uni-link.vue"
	var page = 1,
		timer
	export default {
		components: {
			uniLink
		},
		data() {
			return {
				uid: '',
				token: '',
				company_id: '',
				list: [],
				fileUrl: '',
				progress: '',
				loadingText: '上拉加载更多',
				isShow: false
			}
		},
		onLoad() {
			this.uid = uni.getStorageSync("user").uid
			this.token = uni.getStorageSync("user").token
			this.company_id = uni.getStorageSync("user").company_id
			this.tijiao()
		},
		onReachBottom: function() {
			let that = this
			if (timer != null) {
				clearTimeout(timer)
			}
			timer = setTimeout(function() {
				that.getNewTijiao();
			}, 500);
		},
		methods: {
			tijiao() {
				var that = this
				page = 1
				uni.showNavigationBarLoading();
				this.loadingText = '上拉加载更多'
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/File/index?uid=' + this.uid + '&token=' + this.token + '&id=' + this.company_id +
						'&p=' + page,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						if (res.data.code == 200) {
							console.log(res)
							that.list = res.data.datas
						} else if (res.data.code == 100) {
							console.log(res)
							uni.showToast({
								title: res.data.datas.msg,
								icon: 'none'
							})
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/index/index'
								})
								try {
									uni.removeStorageSync('user');
									console.log('chenggonmg')
								} catch (e) {
									// error
								}
							}, 1500)
						}

					}
				})
			},
			getNewTijiao() {
				var that = this
				if (this.loadingText == '没有更多文件') {
					return false
				}
				this.loadingText = '加载中...'
				uni.showNavigationBarLoading();
				uni.request({
					url: 'https://aqmore.sxpz.vip/AppApi/File/index?uid=' + this.uid + '&token=' + this.token + '&id=' + this.company_id +
						'&p=' + page,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: function(res) {
						uni.hideNavigationBarLoading();
						if (res.data.datas.length == 0) {
							that.loadingText = '没有更多文件';
							return false
						}
						if (res.data.code == 200) {
							console.log(res)
							var newlist = res.data.datas
							that.list = that.list.concat(newlist)
							console.log(that.list)
						}
						page++;
					}
				})
			},
			toDetail(e) {
				console.log(e)
				const _that = this
				if (e == 'https://aqmore.sxpz.vip/Upload/nopic.png') {
					uni.showToast({
						title: '未上传有效内容',
						icon: 'none'
					})
					return false
				}
				var that = this
				console.log(e)
				let file_url = e.replace("http:", "https:")
				console.log(file_url)
				// // #ifdef MP-ALIPAY
				// uni.navigateTo({
				// 	url: '/pages/workspace/workDetial/textMenu/textMenuh5?file='+res.data.datas[e].file_url
				// })
				// // #endif
				console.log(1)
				const downloadTask = uni.downloadFile({
					url: e.replace("http:", "https:"),
					success: function(res) {
						console.log(2)
						console.log(res)
						var filePath = res.tempFilePath;
						// #ifdef MP-WEIXIN
						uni.openDocument({
							filePath: filePath,
							success: function(res) {
								console.log(res)
								console.log('打开文档成功');
							},
							fail: function(res) {
								console.log(res)
							}
						})
						// #endif
						// #ifdef APP-PLUS
						uni.openDocument({
							// fileType:['word'],
							filePath: filePath,
							success: function(res) {
								console.log(res)
								console.log('打开文档成功');
							}
						})
						// #endif
						// uni.saveFile({
						//       tempFilePath:decodeURIComponent(filePath),
						//       success: function (res) {
						//         var savedFilePath = res.savedFilePath;
						//       }
						//     });
					},
					fail: function(res) {
						console.log(res)
					}
				})
				downloadTask.onProgressUpdate((res) => {
					console.log(res.progress)
					_that.progress = res.progress + '%'
					// that.progress=res.progress+'%'
					uni.showLoading({
						title: '文件打开中' + _that.progress
					})
					if (_that.progress == '100%') {
						uni.hideLoading();
						uni.showToast({
							title: '文件打开成功',
							icon: 'success'
						})
					}
					// console.log(that.progress)
					// 测试条件，取消下载任务。
					if (res.progress > 100) {
						downloadTask.abort();

					}
				});


			},


		}
	}
</script>

<style>
	page {
		margin: 0;
		padding: 0;
		/* background-color: #0d98ed; */
	}
	.loading{
		line-height: 2em;text-align: center;color: #808080;margin-top: 30upx;
	}
	.noticeImg{
		width: 360upx;
		height: 300upx;
	}
</style>
